import HFRequest from "./index";
import { useAllStore } from "@/store/modules/base";
import router from "@/router/index";
// 无需token的接口
const arr = [];
// 看接口情况加
// headers: {
//   "Content-Type": "application/json;"
// }
export const hfoRequest = new HFRequest({
  baseURL: import.meta.env.VITE_AXIOS_BASEURL,
  // baseURL: "http://192.168.84.68:8013/prod-api/",
  interceptors: {
    requestInterceptor: (config) => {
      const useStore = useAllStore();
      if (useStore.token && config.headers && config.url && !arr.includes(config.url)) {
        config.headers.Authorization = useStore.token;
      }
      return config;
    },
    requestInterceptorCatch: (err) => {
      return err;
    },
    responseInterceptor: (res) => {
      return res.data;
    },
    responseInterceptorCatch: (error) => {
      let res: Error | undefined;
      if ([401, 403].includes(error.response.status)) {
        return router.push("/login");
      }
      ElMessage.error(error.response.data.message);
      if (res instanceof Error) throw res;
      return error.response;
    }
  }
});
